package GUI;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;

import core.model.Categoria;
import core.model.Empresa;
import core.model.Producto;
import core.model.Subcategoria;

public class panelBusquedaTeclado extends javax.swing.JPanel implements
		ActionListener, ItemListener, MouseListener {

	public static String nombre_producto;
	/**
	 * 
	 */
	private static final long serialVersionUID = 7187046349839564224L;

	public panelBusquedaTeclado() {
		initComponents();
	}

	private void initComponents() {

		bTitulo = new javax.swing.JButton();
        etiquetaNick = new javax.swing.JLabel();
        botonSalir = new javax.swing.JButton();
        panelCentro = new javax.swing.JPanel();
        bAtras = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        eCategoria = new javax.swing.JLabel();
        eSubcategoria = new javax.swing.JLabel();
        cbCategoria = new javax.swing.JComboBox();
        cbSubcategoria = new javax.swing.JComboBox();
        cbSuper = new javax.swing.JComboBox();
        eSuper = new javax.swing.JLabel();
        etiquetaEntrada = new javax.swing.JLabel();
        bBusqueda = new javax.swing.JButton();
        bCrearLista = new javax.swing.JButton();
        bAyuda = new javax.swing.JButton();
        bHistorial = new javax.swing.JButton();
        bSugerencias = new javax.swing.JButton();
        bOpciones = new javax.swing.JButton();

		setBackground(new java.awt.Color(255, 255, 255));

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png")); // NOI18N
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.addActionListener(this);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));

		bSugerencias.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bSugerencias.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonSugerencias.png")); // NOI18N
		bSugerencias.setBorderPainted(false);
		bSugerencias.addActionListener(this);
		bSugerencias.setBackground(new java.awt.Color(255, 255, 255));

		bCrearLista.setBackground(new java.awt.Color(255, 255, 255));
		bCrearLista.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bCrearLista.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonCrearLista.png")); // NOI18N
		bCrearLista.setBorderPainted(false);
		bCrearLista.addActionListener(this);

		bHistorial.setBackground(new java.awt.Color(255, 255, 255));
		bHistorial.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bHistorial.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonHistorial.png")); // NOI18N
		bHistorial.setBorderPainted(false);
		bHistorial.addActionListener(this);

		etiquetaNick.setBackground(new java.awt.Color(102, 102, 102));
		etiquetaNick.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaNick.setText(panelInicio.nick);
		etiquetaNick.setToolTipText("");

		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.addActionListener(this);
		botonSalir.setBackground(new java.awt.Color(255, 255, 255));

		panelCentro.setBackground(new java.awt.Color(255, 255, 255));

		bAtras.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/boton_atras.png")); // NOI18N
		bAtras.setBorder(null);
		bAtras.addActionListener(this);
		bAtras.setBackground(new java.awt.Color(255, 255, 255));

		jTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {
				{ null, null, null, null }, { null, null, null, null },
				{ null, null, null, null }, { null, null, null, null } },
				new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
		
		jTable.addMouseListener(new MouseAdapter() 
		   {
		      public void mouseClicked(MouseEvent e) 
		      {
		         int fila = jTable.rowAtPoint(e.getPoint());
		         String  producto=(jTable.getValueAt(fila, 0).toString());
		         nombre_producto=producto;
		         cbSuper.updateUI();
		         List<String> listaNombresEmpresas = new ArrayList<String>();
					String codigo_barras = buscarProducto(nombre_producto).getCodigo_barras();
					List<Empresa> empresas1 = obtenerEmpresasByCodigoProducto(codigo_barras);
								
					Iterator<Empresa> iterator = empresas1.iterator();
					while (iterator.hasNext()) {
						Empresa empresa = iterator.next();
						listaNombresEmpresas.add(String.valueOf(obtenerEmpresasByCodigoEmpresa(empresa.getCodigo_empresa()).getNombre())+"  "+obtenerPrecio(empresa.getCodigo_empresa(), codigo_barras).getPrecio());
					}
					String[] empresasArrayString = new String[listaNombresEmpresas.size()];
					listaNombresEmpresas.toArray(empresasArrayString);

					cbSuper.setModel(new javax.swing.DefaultComboBoxModel<String>(
							empresasArrayString));
					// FIN
					cbSuper.setEnabled(false);
					cbSuper.setSelectedIndex(0);
					cbSuper.updateUI();
		        
		         int columna = jTable.columnAtPoint(e.getPoint());
		         if ((fila > -1) && (columna > -1)){
		        	 cbSuper.setEnabled(true);
		        	 
		         }
		          
		      }
		   });
		jScrollPane1.setViewportView(jTable);

		

		eCategoria.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eCategoria.setForeground(new java.awt.Color(102, 102, 102));
		eCategoria.setText("CATEGORIA");

		eSubcategoria.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eSubcategoria.setForeground(new java.awt.Color(102, 102, 102));
		eSubcategoria.setText("SUBCATEGORIA");
		
		 eSuper.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
	     eSuper.setForeground(new java.awt.Color(102, 102, 102));
	     eSuper.setText("SUPER/PRECIO");
	     
		

		// RELLENAR COMBOBOX
		List<String> listaNombresCategorias = new ArrayList<String>();
		List<Categoria> categorias = (List<Categoria>) obtenerCategorias();

		Iterator<Categoria> iterator = categorias.iterator();
		while (iterator.hasNext()) {
			Categoria categoria = iterator.next();
			listaNombresCategorias.add(String.valueOf(categoria.getNombre()));
		}
		String[] categoriasArrayString = new String[listaNombresCategorias
				.size()];
		listaNombresCategorias.toArray(categoriasArrayString);

		cbCategoria.setModel(new javax.swing.DefaultComboBoxModel<String>(
				categoriasArrayString));
		// FIN
		cbCategoria.addItemListener(this);

		org.jdesktop.layout.GroupLayout panelCentroLayout = new org.jdesktop.layout.GroupLayout(panelCentro);
        panelCentro.setLayout(panelCentroLayout);
        panelCentroLayout.setHorizontalGroup(
            panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentroLayout.createSequentialGroup()
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, panelCentroLayout.createSequentialGroup()
                        .add(eCategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cbCategoria, 0, 384, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, panelCentroLayout.createSequentialGroup()
                        .add(eSubcategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cbSubcategoria, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .add(56, 56, 56))
            .add(panelCentroLayout.createSequentialGroup()
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelCentroLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(bAtras))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 601, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(panelCentroLayout.createSequentialGroup()
                        .add(eSuper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 173, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cbSuper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 203, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(0, 0, Short.MAX_VALUE))
        );
        panelCentroLayout.setVerticalGroup(
            panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentroLayout.createSequentialGroup()
                .add(63, 63, 63)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eCategoria)
                    .add(cbCategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eSubcategoria)
                    .add(cbSubcategoria, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(28, 28, 28)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eSuper)
                    .add(cbSuper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(44, 44, 44)
                .add(bAtras)
                .addContainerGap(39, Short.MAX_VALUE))
        );
		bOpciones.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bOpciones.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonOpciones.png")); // NOI18N
		bOpciones.setBorderPainted(false);
		bOpciones.addActionListener(this);
		bOpciones.setBackground(new java.awt.Color(255, 255, 255));

		etiquetaEntrada.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaEntrada.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaEntrada.setText("BIENVENIDO A LISTA DE LA COMPRA MAKER ");

		bBusqueda.setBackground(new java.awt.Color(0, 102, 0));
		bBusqueda.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bBusqueda.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonBusqueda.png")); // NOI18N
		bBusqueda.setToolTipText("");
		bBusqueda.setBorderPainted(false);
		bBusqueda.addActionListener(this);
		bBusqueda.setBackground(new java.awt.Color(255, 255, 255));

		bAyuda.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bAyuda.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonAyuda.png")); // NOI18N
		bAyuda.setBorderPainted(false);
		bAyuda.addActionListener(this);
		bAyuda.setBackground(new java.awt.Color(255, 255, 255));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.TRAILING)
												.add(bCrearLista,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														157,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.add(bHistorial,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														157,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.add(bSugerencias,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														157,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.add(bOpciones,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														157,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(org.jdesktop.layout.GroupLayout.TRAILING,
												bAyuda,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												157,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(bBusqueda,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										155,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.add(panelCentro,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
				.add(org.jdesktop.layout.GroupLayout.TRAILING,
						layout.createSequentialGroup()
								.add(layout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(layout.createSequentialGroup()
												.add(96, 96, 96).add(bTitulo)
												.add(44, 44, 44)
												.add(botonSalir))
										.add(layout
												.createSequentialGroup()
												.add(84, 84, 84)
												.add(etiquetaEntrada)
												.add(46, 46, 46)
												.add(etiquetaNick,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														280,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(20, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(bTitulo,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										104,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(botonSalir))
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(etiquetaEntrada).add(etiquetaNick))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(bBusqueda,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bCrearLista,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bHistorial,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bSugerencias,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bOpciones,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bAyuda,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(panelCentro,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
	}
	
	private Subcategoria obtenerSubcategoria (String nombre_subcategoria) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		Subcategoria subcategoria = ventanaManager.getSubcategoriaByName(nombre_subcategoria);
		return subcategoria;
	}

	private List<Categoria> obtenerCategorias() {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Categoria> listaCategorias = ventanaManager.getAllCategorias();
		return listaCategorias;
	}

	private List<Subcategoria> obtenerSubcategorias(int cod_categoria) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Subcategoria> subcategorias = ventanaManager
				.getSubcategoriaByCodeCategoria(cod_categoria);
		return subcategorias;
	}

	private javax.swing.JButton bAtras;
    private javax.swing.JButton bAyuda;
    private javax.swing.JButton bBusqueda;
    private javax.swing.JButton bCrearLista;
    private javax.swing.JButton bHistorial;
    private javax.swing.JButton bOpciones;
    private javax.swing.JButton bSugerencias;
    private javax.swing.JButton bTitulo;
    private javax.swing.JButton botonSalir;
    private javax.swing.JComboBox cbCategoria;
    private javax.swing.JComboBox cbSubcategoria;
    private javax.swing.JComboBox cbSuper;
    private javax.swing.JLabel eCategoria;
    private javax.swing.JLabel eSubcategoria;
    private javax.swing.JLabel eSuper;
    private javax.swing.JLabel etiquetaEntrada;
    private javax.swing.JLabel etiquetaNick;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    private javax.swing.JPanel panelCentro;
    private Point punto=new Point();
	private JFrame actual;
	private JPanel estePanel = this;

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == bAtras) {

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelBusqueda());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}
		if (e.getSource() == bSugerencias) {

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSugerencias());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
		}
		if (e.getSource() == bHistorial) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelHistorial());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bOpciones) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelOpciones());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAyuda) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAyuda());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bCrearLista) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelCrearLista());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(8));
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
	}
	
	private List<Producto> getAllProductosBySubcategoria(int codigo_subcategoria) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Producto> productos = ventanaManager.getAllProductos(codigo_subcategoria);
		return productos;
	}
	
	private String[][] convertirArray (List<Producto> productos) {
		Producto productoActual;
		String[][] aProductos = new String[productos.size()][4];

		for(int i = 0; i<productos.size(); i++) {
			productoActual = productos.get(i);
			aProductos[i][0] = productoActual.getNombre();
			aProductos[i][1] = productoActual.getDescripcion();
			aProductos[i][2] = String.valueOf(productoActual.getCantidad());
			aProductos[i][3] = productoActual.getUnidad();
		}
		
		return aProductos;
	}

	public void itemStateChanged(ItemEvent e) {
		
		if (e.getSource() == cbCategoria) {

			/*if (cbCategoria.getSelectedIndex() != 0) {

				bBuscarSubcategoria.setEnabled(true);
			}
			if (cbCategoria.getSelectedIndex() == 0) {
				bBuscarSubcategoria.setEnabled(false);
			}
*/
			cbSubcategoria.addItemListener(this);
			cbSubcategoria.setEnabled(true);

			String nombreCategoria = (String) cbCategoria.getSelectedItem();
			System.out.println(nombreCategoria);
			VentanaManager ventanaManager = VentanaFactory.getInstance();
			Categoria categoria = ventanaManager
					.getCategoriaByName(nombreCategoria);

			// RELLENAR COMBOBOX SUBCATEGORIA
			List<String> listaNombresSubcategorias = new ArrayList<String>();
			List<Subcategoria> subcategorias = obtenerSubcategorias(categoria
					.getCodigo_categoria());
			
			Iterator<Subcategoria> iterator = subcategorias.iterator();
			while (iterator.hasNext()) {
				Subcategoria subcategoria = iterator.next();
				listaNombresSubcategorias.add(String.valueOf(subcategoria
						.getNombre()));
			}
			String[] SubcategoriasArrayString = new String[listaNombresSubcategorias
					.size()];
			listaNombresSubcategorias.toArray(SubcategoriasArrayString);

			cbSubcategoria.setModel(new javax.swing.DefaultComboBoxModel(
					SubcategoriasArrayString));
			// FIN
		}
		
		if (e.getSource() == cbSubcategoria) {
			String nombre_subcategoria = (String) cbSubcategoria.getSelectedItem();
			Subcategoria subcategoria = obtenerSubcategoria(nombre_subcategoria);
			List<Producto> productos = getAllProductosBySubcategoria(subcategoria.getCodigo_subcategoria());
			
			//cabeceras
			Object[] cabeceras = new Object[]{"Nombre", "Descripcion", "Cantidad", "Unidad"};
			//cambiar jTable
			DefaultTableModel dtm = new DefaultTableModel(convertirArray(productos), cabeceras);
			jTable.setModel(dtm);
			System.out.println("FUNCIONA");
			
			//String nombre_producto = (String) dtm.getValueAt(jTable.getSelectedRow(), 0);
			//System.out.println("NOMBRE PRODUCTO"+nombre_producto);
			
			//int fila=jTable.getSelectedRow();
			//int colum=jTable.getSelectedColumn();
			//System.out.println("Fila y columna"+ fila + colum);
			
			String nombre_producto=(String) dtm.getValueAt(jTable.columnAtPoint(punto),jTable.rowAtPoint(punto));
			System.out.println(punto);
			
			// RELLENAR COMBOBOX
			List<String> listaNombresEmpresas = new ArrayList<String>();
			String codigo_barras = buscarProducto(nombre_producto).getCodigo_barras();
			List<Empresa> empresas1 = obtenerEmpresasByCodigoProducto(codigo_barras);
						
			Iterator<Empresa> iterator = empresas1.iterator();
			while (iterator.hasNext()) {
				Empresa empresa = iterator.next();
				listaNombresEmpresas.add(String.valueOf(obtenerEmpresasByCodigoEmpresa(empresa.getCodigo_empresa()).getNombre())+"  "+obtenerPrecio(empresa.getCodigo_empresa(), codigo_barras).getPrecio());
			}
			String[] empresasArrayString = new String[listaNombresEmpresas.size()];
			listaNombresEmpresas.toArray(empresasArrayString);

			cbSuper.setModel(new javax.swing.DefaultComboBoxModel<String>(
					empresasArrayString));
			// FIN
			cbSuper.setEnabled(false);
			cbSuper.setSelectedIndex(0);
		}

	}
	
	private Empresa obtenerEmpresasByCodigoEmpresa(int codigo_empresa) {
		// TODO Auto-generated method stub
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		Empresa empresa = ventanaManager.findCodigoEmpresa(codigo_empresa);
		return empresa;
	}
	
	private Producto buscarProducto(String nombre_producto) {
		Producto producto;
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		producto = ventanaManager.findProductoByNombre(nombre_producto);
		return producto;
	}
	
	private List<Empresa> obtenerEmpresasByCodigoProducto(String codigo_barras) {
		// TODO Auto-generated method stub
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		List<Empresa> empresas = ventanaManager.findByCodigoProducto(codigo_barras);
		return empresas;
		
	}
	private Empresa obtenerPrecio (int codigo_empresa, String codigo_producto) {
		VentanaManager ventanaManager = VentanaFactory.getInstance();
		Empresa empresa = ventanaManager.findPrecio(codigo_empresa, codigo_producto);
		return empresa;
	}

	@Override
	public void mouseClicked(MouseEvent me) {
		
		if(me.getSource()==jTable){
			if(me.getSource()==jTable){
				int fila=jTable.rowAtPoint(me.getPoint());
				punto=me.getPoint();
				
				
			}
			
		}
			
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
